package com.dq.demo.ui.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dq.demo.ui.activity.AboutActivity;
import com.dq.demo.ui.activity.FeedBackActivity;
import com.dq.demo.ui.activity.UserServiceAgreementActivity;
import com.dq.demo.util.CleanDataUtils;
import com.dq.demo.util.ILog;
import com.dq.demo.util.SystemUtil;
import com.dq.demo.util.ToastUtil;
import com.dq.wallpaper.R;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class MeFragment extends BaseFragment {

    @BindView(R.id.img_back)
    public ImageView imgBack;
    @BindView(R.id.tv_title)
    public TextView tvTitle;
    @BindView(R.id.img_title)
    public ImageView imgTitle;
    @BindView(R.id.fl_top_ad)
    public FrameLayout flTopAd;
    @BindView(R.id.rl_2)
    public RelativeLayout rl2;
    @BindView(R.id.rl_3)
    public RelativeLayout rl3;
    @BindView(R.id.rl_4)
    public RelativeLayout rl4;
    @BindView(R.id.rl_5)
    public RelativeLayout rl5;
    @BindView(R.id.rl_6)
    public RelativeLayout rl6;
    @BindView(R.id.rl_7)
    public RelativeLayout rl7;
    @BindView(R.id.rl_8)
    public RelativeLayout rl8;
    @BindView(R.id.rl_9)
    public RelativeLayout rl9;
    @BindView(R.id.title)
    public RelativeLayout rlTitle;
    @BindView(R.id.tv_cache)
    public TextView tvCache;
    @BindView(R.id.tv_version)
    public TextView tvVersion;

    public Unbinder unbinder;

    public static MeFragment createFramgemt(){
        return new MeFragment();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(getLayoutId(), container, false);
        unbinder = ButterKnife.bind(this, mView);
        mIsViewCreated = true;
        return mView;
    }

    public void initView() {
        tvTitle.setText("我的");
        tvVersion.setText("V" + SystemUtil.getVersionName());
        rlTitle.setPadding(0, height, 0, 0);
        imgBack.setVisibility(View.GONE);
        imgTitle.setVisibility(View.GONE);

        try {
            String totalCacheSize = CleanDataUtils.getTotalCacheSize(activity);
            tvCache.setText(totalCacheSize);
        } catch (Exception e) {
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_me;
    }

    @Override
    public void initData() {
        onVisible();
    }

    @Override
    public void onVisible() {
        super.onVisible();
    }

    @Override
    public void beforeDestroy() {}

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.rl_2, R.id.rl_3, R.id.rl_4, R.id.rl_5, R.id.rl_6, R.id.rl_7, R.id.rl_8, R.id.rl_9})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_2://修改年级
                break;
            case R.id.rl_3://收藏夹
                break;
            case R.id.rl_4://清理缓存
                new QMUIDialog.MessageDialogBuilder(activity)
                        .setMessage("确认清理吗?")
                        .setCancelable(false)//禁止按下返回false
                        .setCanceledOnTouchOutside(false)//禁止触摸false
                        .addAction("取消", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                dialog.dismiss();
                            }
                        }).addAction("确认", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                        CleanDataUtils.clearAllCache(activity);//清理缓存
                        try {//重新计算缓存大小
                            String totalCacheSize = CleanDataUtils.getTotalCacheSize(activity);
                            tvCache.setText(totalCacheSize);
                        } catch (Exception e) {
                            ILog.e("e:" + e.toString());
                        }
                    }
                }).show();
                break;
            case R.id.rl_5://隐私政策
                UserServiceAgreementActivity.startAct(activity, 1);
                break;
            case R.id.rl_6://用户协议
                UserServiceAgreementActivity.startAct(activity, 0);
                break;
            case R.id.rl_7://版本信息
                ToastUtil.showShortToast(activity, "当前已是最新版本");
                break;
            case R.id.rl_8://关于我们
                startAct(activity, new AboutActivity());
                break;
            case R.id.rl_9://用户反馈
                startAct(activity,new FeedBackActivity());
                break;
        }
    }
}